70C - Lucky Tickets - CodeForces Solution


binary search data structures sortings two pointers *2200

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int rev(int x) {
    int xx = 0;
    while(x) {
        xx = xx*10+x%10;
        x /= 10;
    }
    return xx;
}

int main() {
    int mxx, mxy, w;
    cin>>mxx>>mxy>>w;
    map<double, int> cA, cB;
    for(int a=1; a<=mxx; a++) {
        int gcd = __gcd(a, rev(a));
        cA[a/gcd*1.0/(rev(a)/gcd)]++;
    }
    cB[1] = 1;
    int b = 1, x = -1, y = -1;
    long long ans = cA[1], mn = LLONG_MAX;
    for(int a=mxx; a>=1; a--) {
        while(b<=mxy && ans<w) {
            b++;
            int gcd = __gcd(b, rev(b));
            ans += cA[rev(b)/gcd*1.0/(b/gcd)];
            cB[rev(b)/gcd*1.0/(b/gcd)]++;
        }
        if(b==mxy+1) break;
        if(a*1LL*b<mn) mn = a*1LL*b, x = a, y = b;
        int gcd = __gcd(a, rev(a));
        ans -= cB[a/gcd*1.0/(rev(a)/gcd)];
        cA[a/gcd*1.0/(rev(a)/gcd)]--;
    }
    if(x==-1) cout<<"-1\n";
    else cout<<x<<" "<<y<<"\n";
    return 0;
}/*1690966119.9241564*/


Comments

Submit
0 Comments
More Questions

190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND